package my.suveng.nettyhello.module.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * create at 2018/10/20
 * author: suveng
 * email: suveng@163.com
 **/
public class HelloServer {
    public static void main(String[] args) throws InterruptedException {
//        1. 定义主线程组，用于接受客户端连接
        EventLoopGroup bossGroup = new NioEventLoopGroup();
//        2. 定义从线程组，用于处理逻辑
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
//        启动类
            ServerBootstrap serverBootstrap = new ServerBootstrap();
//        启动类设置属性
            serverBootstrap.group(bossGroup, workerGroup)//指定主从线程模型
                    .channel(NioServerSocketChannel.class)//这里设置Nio channel
                    .childHandler(new HelloServerInitializer());//定义处理器，这里设置为自定义的HelloServerInitializer

//        启动类监听端口并启动
            ChannelFuture channelFuture = serverBootstrap.bind(8088)//监听端口
                    .sync();//同步方式
//        关闭
//            channelFuture.channel()//获取channel
//                    .close()//关闭channel
//                    .sync();//同步方式
        }finally {
//            以优雅的方式关闭服务器
//            bossGroup.shutdownGracefully();
        }

    }
}
